package com.pinyougou.seller.service;

import com.pinyougou.pojo.TbSeller;
import com.pinyougou.sellergoods.service.SellerService;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import java.util.ArrayList;
import java.util.List;

/**
 * 得到用户名信息
 */
public class UserServiceImpl implements UserDetailsService {
    private SellerService sellerService;

    public void setSellerService(SellerService sellerService) {
        this.sellerService = sellerService;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //1、根据商家名称得到商家对象
        TbSeller seller = sellerService.findOne(username);
        System.out.println("根据商家名称得到商家对象:"+seller);
        //2、定义角色列表
        List<GrantedAuthority> authorityList = new ArrayList<>();
        if (seller != null){
            if (seller.getStatus().equals("1")){//审核通过
                System.out.println("此商家审核通过了");
                //3、添加角色到角色列表中
                authorityList.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
                //4、构造用户对象并返回
                User user = new User(username, seller.getPassword(), authorityList);
                System.out.println("当前正在登录的用户"+user.getUsername());
                return user;
            }
            return null;
        }
        return null;
    }
}
